package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.hitqz.robot.api.common.entity.AreaAction;
import com.hitqz.robot.api.common.entity.MapAreaAction;
import com.hitqz.robot.api.business.model.vo.mapArea.AreaActionVo;
import com.hitqz.robot.api.business.model.param.mapArea.MapAreaActionParams;
import com.hitqz.robot.api.business.model.vo.mapArea.MapAreaActionVo;
import com.hitqz.robot.biz.service.AreaActionService;
import com.hitqz.robot.biz.service.MapAreaActionService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author xupkun
 * @date 2024/6/3
 */
@RestController
@AllArgsConstructor
@RequestMapping("/areaAction")
@Tag(name = "区域动作")
public class AreaActionController {

    private final AreaActionService areaActionService;

    private final MapAreaActionService mapAreaActionService;

    @GetMapping
    @Operation(summary = "获取所有的动作列表")
    public R<List<AreaActionVo>> getList() {
        List<AreaAction> areaActions = areaActionService.list();
        List<AreaActionVo> vos = Lists.newArrayList();
        for (AreaAction areaAction: areaActions) {
            AreaActionVo vo = new AreaActionVo();
            BeanUtil.copyProperties(areaAction, vo);
            vos.add(vo);
        }
        return R.ok(vos);
    }


    @GetMapping(value = "getActions")
    @Operation(summary = "获取区域的动作列表")
    public R<List<MapAreaActionVo>> getActions(@Parameter(description = "区域id")
                                                   @RequestParam(value = "areaId") Integer areaId) {
        List<MapAreaAction> areaActions = mapAreaActionService.list(Wrappers.<MapAreaAction>lambdaQuery()
                .eq(MapAreaAction::getAreaId, areaId));
        List<MapAreaActionVo> vos = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(areaActions)) {
            for (MapAreaAction areaAction: areaActions) {
                MapAreaActionVo vo = new MapAreaActionVo();
                BeanUtil.copyProperties(areaAction, vo);
                vo.setName(Optional.ofNullable(areaActionService.getById(areaAction.getActionId())).orElse(new AreaAction()).getName());
                vos.add(vo);
            }
        }
        return R.ok(vos);
    }


    @PostMapping
    @Operation(summary = "添加动作")
    public R save(@RequestBody List<MapAreaActionParams> params) {
        // 先删除，所有的动作数据
        mapAreaActionService.remove(Wrappers.<MapAreaAction>lambdaQuery().eq(MapAreaAction::getAreaId, params.get(0).getAreaId()));
        for (MapAreaActionParams list: params) {
            MapAreaAction mapAreaAction = new MapAreaAction();
            BeanUtil.copyProperties(list, mapAreaAction);
            mapAreaActionService.save(mapAreaAction);
        }
        return R.ok();
    }


    @DeleteMapping
    @Operation(summary = "删除地图上区域的动作")
    public R delete(@RequestBody Integer[] ids) {
        for(Integer id : ids) {
            mapAreaActionService.removeById(id);
        }
        return R.ok();
    }

}
